﻿function validate_event(type) {
      var valid = true;
      if(document.getElementById(type+'everror_title')) {
        document.getElementById(type+'everror_title').style.display = 'none';
        if(document.getElementById(type+'evtitle').value == "") {
          document.getElementById(type+'everror_title').innerHTML = JS_NAMEEVENT;
          document.getElementById(type+'everror_title').style.display = 'block';
          valid = false;
        }
        document.getElementById(type+'everror_start').style.display = 'none';
        if((document.getElementById(type+'evstartdate').value == "") || (!document.getElementById(type+'evstartdate').value.match(/^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/(0?[13578]|1?[02]))|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$/))) {
            document.getElementById(type+'everror_start').innerHTML = JS_STARTDATE;
            document.getElementById(type+'everror_start').style.display = 'block';
            valid = false;
        } else {
  	      if(document.getElementById(type+'evstartminute') != null) {
            if(document.getElementById(type+'evstartminute').value == "") {
              document.getElementById(type+'evstartminute').value = "00";
            }
            if(document.getElementById(type+'evendhour').value == "") {
              document.getElementById(type+'evendhour').value = document.getElementById(type+'evstarthour').value;
            }
            if(document.getElementById(type+'evendminute').value == "") {
              document.getElementById(type+'evendminute').value = document.getElementById(type+'evstartminute').value;
            }
            document.getElementById(type+'everror_allday').style.display = 'none';
            if(!document.getElementById(type+'evallday').checked) {
              if((document.getElementById(type+'evstarthour').value == "") || (document.getElementById(type+'evstartminute').value == "")) {
                document.getElementById(type+'everror_allday').innerHTML = JS_ALLDAY;
                document.getElementById(type+'everror_allday').style.display = 'block';
                valid = false;
              }
            }
            document.getElementById(type+'everror_end').style.display = 'none';
            if((document.getElementById(type+'evenddate').value == "") || (!document.getElementById(type+'evenddate').value.match(/^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/(0?[13578]|1?[02]))|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$/))) {
              document.getElementById(type+'everror_end').innerHTML = JS_ENDDATE;
              document.getElementById(type+'everror_end').style.display = 'block';
              valid = false;
            }
          }
        }
        if(document.getElementById(type+'error_xdays') != null) {
            document.getElementById(type+'error_xdays').style.display = 'none';
            if((document.addeventform.everyxdays.checked) && (!document.addeventform.xdays.value.match(/^([1-9]{1,2}|([1-9]{1}[0-9]{0,1}))$/))) {
                document.getElementById(type+'error_xdays').innerHTML = JS_OCCURRENCES
                document.getElementById(type+'error_xdays').style.display = 'block';
                valid = false;
            }
        }
        if(document.getElementById(type+'error_rec1') != null) {
            document.getElementById(type+'error_rec1').style.display = 'none';
            if((document.addeventform.rectype1.checked) && (!document.addeventform.numrecurrences.value.match(/^([1-9]{1,2}|([1-9]{1}[0-9]{0,1}))$/))) {
                document.getElementById(type+'error_rec1').innerHTML = JS_NUMOCCURRENCES;
                document.getElementById(type+'error_rec1').style.display = 'block';
                valid = false;
            }
        }
        if((document.getElementById(type+'error_rec3') != null) && (document.addeventform.enddate != null)) {
            document.getElementById(type+'error_rec3').style.display = 'none';
            if((document.addeventform.rectype3.checked) && (!document.addeventform.enddate.value.match(/^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/(0?[13578]|1?[02]))|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$/))) {
                document.getElementById(type+'error_rec3').innerHTML = JS_STARTDATE;
                document.getElementById(type+'error_rec3').style.display = 'block';
                valid = false;
            }
        }
        if(document.getElementById(type+'error_recyear') != null) {
            document.getElementById(type+'error_recyear').style.display = 'none';
            if((document.addeventform.yeartype1.checked) && (!document.addeventform.yearlydate.value.match(/^([1-9]{1}|[12]{1}[0-9]{1}|30|31)$/))) {
                document.getElementById(type+'error_recyear').innerHTML = JS_DAYMONTH;
                document.getElementById(type+'error_recyear').style.display = 'block';
                valid = false;
            }
        }
      }
      return valid;
}
function validate_search() {
    var valid = true;
    if(document.getElementById('search')) {
        if(document.getElementById('searchtitle').value.length == 0) {
            if(document.getElementById('findevents').style.display != 'none') {
                if(document.getElementById('searchvenue').value.length == 0) {
                    document.getElementById('searcherror_title').innerHTML = JS_NOSEARCHTITLE;
                    document.getElementById('searcherror_title').style.display = 'block';
                    valid = false;
                }
            } else {
                document.getElementById('searcherror_title').innerHTML = JS_NOSEARCHTITLE;
                document.getElementById('searcherror_title').style.display = 'block';
                valid = false;
            }
        }
    }
    return valid;
}
function validate_reg() {
      var valid = true;
      if(document.getElementById('username')) {
            document.getElementById('error_username').style.display = 'none';
            if(document.getElementById('username').value == "") {
              document.getElementById('error_username').innerHTML = JS_USERBLANK;
              document.getElementById('error_username').style.display = 'block';
              valid = false;
            }
            document.getElementById('error_pass').style.display = 'none';
            if(document.getElementById('pass').value == "") {
              document.getElementById('error_pass').innerHTML = JS_PASSBLANK;
              document.getElementById('error_pass').style.display = 'block';
              valid = false;
            }
            if(document.getElementById('pass').value != document.getElementById('confpass').value) {
              document.getElementById('error_pass').innerHTML = JS_CONFIRMPASSFAIL;
              document.getElementById('error_pass').style.display = 'block';
              valid = false;
            }

      }
      return valid;
}
function validate_todo(type) {
          var valid = true;
          document.getElementById(type+'taskerror_taskname').style.display = 'none';
          if(document.getElementById(type+'taskname').value == "") {
            document.getElementById(type+'taskerror_taskname').innerHTML = JS_TASKNAME;
            document.getElementById(type+'taskerror_taskname').style.display = 'block';
            valid = false;
          }
          document.getElementById(type+'taskerror_datedue').style.display = 'none';
          if((document.getElementById(type+'taskdatedue').value != "") && (!document.getElementById(type+'taskdatedue').value.match(/^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/(0?[13578]|1?[02]))|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$/))) {
              document.getElementById(type+'taskerror_datedue').innerHTML = JS_DATEDUE;
              document.getElementById(type+'taskerror_datedue').style.display = 'block';
              valid = false;
          }
        return valid;
}
function validate_mass() {
var valid = true;
    for(var e = 0;e<jsevents.length;e++) {
      var itemtype = jsevents[e].substring(0,1);
      var itemid = jsevents[e].substring(1,7);
      if(itemtype == 'e') {
        document.getElementById(itemid+'error_title').style.display = 'none';
          if(document.getElementById(itemid+'evtitle').value == "") {
            document.getElementById(itemid+'error_title').innerHTML = JS_NAMEEVENT;
            document.getElementById(itemid+'error_title').style.display = 'block';
            valid = false;
          }
        document.getElementById(itemid+'error_start').style.display = 'none';
        if((document.getElementById(itemid+'startdate').value == "") || (!document.getElementById(itemid+'startdate').value.match(/^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/(0?[13578]|1?[02]))|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$/))) {
            document.getElementById(itemid+'error_start').innerHTML = JS_STARTDATE;
            document.getElementById(itemid+'error_start').style.display = 'block';
            valid = false;
        }
        if(document.getElementById(itemid+'error_end') != null) {
            document.getElementById(itemid+'error_allday').style.display = 'none';
            if(!document.getElementById(itemid+'allday').checked) {
              if((document.getElementById(itemid+'starthour').value == "") || (document.getElementById(itemid+'startminute').value == "")) {
                document.getElementById(itemid+'error_allday').innerHTML = JS_ALLDAY;
                document.getElementById(itemid+'error_allday').style.display = 'block';
                valid = false;
              }
            }
            document.getElementById(itemid+'error_end').style.display = 'none';
            if((document.getElementById(itemid+'enddate').value == "") || (!document.getElementById(itemid+'enddate').value.match(/^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/(0?[13578]|1?[02]))|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$/))) {
                document.getElementById(itemid+'error_end').innerHTML = JS_ENDDATE;
                document.getElementById(itemid+'error_end').style.display = 'block';
                valid = false;
            }
            var startsecs = parseInt((document.getElementById(itemid+'starthour').value * 60) + (document.getElementById(itemid+'startminute').value));
            var endsecs = parseInt((document.getElementById(itemid+'endhour').value * 60) + (document.getElementById(itemid+'endminute').value));
            if(startsecs > endsecs) {
                document.getElementById(itemid+'error_end').innerHTML = JS_STARTEND;
                document.getElementById(itemid+'error_end').style.display = 'block';
                valid = false;
            }
        }
      } else if(itemtype == 't') {
          document.getElementById(itemid+'error_taskname').style.display = 'none';
            if(document.getElementById(itemid+'taskname').value == "") {
              document.getElementById(itemid+'error_taskname').innerHTML = JS_TASKNAME;
              document.getElementById(itemid+'error_taskname').style.display = 'block';
              valid = false;
            }
          document.getElementById(itemid+'error_datedue').style.display = 'none';
          if((document.getElementById(itemid+'datedue').value != "") && (!document.getElementById(itemid+'datedue').value.match(/^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/(0?[13578]|1?[02]))|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$/))) {
              document.getElementById(itemid+'error_datedue').innerHTML = JS_DATEDUE;
              document.getElementById(itemid+'error_datedue').style.display = 'block';
              valid = false;
          }
      }
    }
    return valid;
}